//
//  FZHomeBannerCollectionViewCell.m
//  EnglishTalk
//
//  Created by CyonLeuPro on 15/6/26.
//  Copyright (c) 2015年 Feizhu Tech. All rights reserved.
//

#import "FZHomeBannerCollectionViewCell.h"
#import "FZHomeBannerImageSwipeViewCell.h"
#import "UIImageView+FZDefaultPlaceholder.h"
#import "FZStyleSheet.h"

#import <FZPagingSwipeView.h>
//#import "FZHomeSliderModel.h"
#import "FZAdvertisementModel.h"

static NSTimeInterval const FZHomeBannerAutoscrollTimeInterval = 3.0;


@interface FZHomeBannerCollectionViewCell () <SwipeViewDataSource, SwipeViewDelegate>

@property (assign, nonatomic) IBOutlet FZPagingSwipeView *swipeView;
@property (assign, nonatomic) IBOutlet UIPageControl *pageControl;





@end

@implementation FZHomeBannerCollectionViewCell


- (void)awakeFromNib {
    // Initialization code
    [super awakeFromNib];
    
    [self setupView];
}

- (void)setupView {
//    FZPagingSwipeView *swipeView = [[FZPagingSwipeView alloc] initWithFrame:self.bannerView.bounds];
    self.swipeView.pagingEnabled = YES;
    self.swipeView.wrapEnabled = YES;
    
//    self.swipeView.pagingAutoscrollDuration = FZHomeBannerAutoscrollTimeInterval;
    self.swipeView.dataSource = self;
    self.swipeView.delegate = self;
    
    _bannerPage = 0;
    FZStyleSheet *css = [FZStyleSheet currentStyleSheet];
    self.pageControl.currentPageIndicatorTintColor =css.colorOfLighterText;
    self.pageControl.pageIndicatorTintColor = [UIColor colorWithRed:203/255.0 green:203/255.0 blue:203/255.0 alpha:0.5];

    self.pageControl.currentPage = _bannerPage;
    
    //self.titleLabel.text = @"";
    //self.titleLabel.textColor = [UIColor whiteColor];
}

- (void)updateCellInfo:(id)data {
    [super updateCellInfo:data];
    
    if (!data || ![data isKindOfClass:[FZHomeSectionModel class]]) {
        return;
    }
    
    FZHomeSectionModel *model = (FZHomeSectionModel *)data;
    self.items = model.sliders;
    self.pageControl.numberOfPages = model.sliders.count;
    self.pageControl.currentPage = _bannerPage;
    [self updateTitleForIndex:_bannerPage];
    
    self.swipeView.pagingAutoscrollDuration = self.pageControl.numberOfPages < 2 ? 0.0f : FZHomeBannerAutoscrollTimeInterval;
    
    [self refreshSwipeView];
}

- (void)refreshSwipeView {
    [self.swipeView reloadData];
}


- (void)updateTitleForIndex:(NSInteger)index {
    if (index < self.items.count) {
        FZAdvertisementModel *item = self.items[index];
        self.titleLabel.text = item.title;
    }
}

#pragma mark - SwipeViewDataSource

- (NSInteger)numberOfItemsInSwipeView:(SwipeView *)swipeView {
    return self.items.count;
}
- (UIView *)swipeView:(SwipeView *)swipeView viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view {
    FZHomeBannerImageSwipeViewCell *cell;
    if ([view isKindOfClass:[FZHomeBannerImageSwipeViewCell class]]) {
        cell = (FZHomeBannerImageSwipeViewCell *)view;
        [cell zoomImageWithNewHeight:CGRectGetHeight(self.bannerView.frame)];
    } else {
        cell = [[FZHomeBannerImageSwipeViewCell alloc] initWithSize:self.swipeView.frame.size imageSize:self.bannerView.frame.size];
//        cell.backgroundColor = [UIColor whiteColor];
        
    }
    
    FZAdvertisementModel *item = self.items[index];
    [cell setImageURL:[NSURL URLWithString:item.imageUrlString]];
    
    return cell;
}

# pragma mark - SwipeViewDelegate

- (CGSize)swipeViewItemSize:(SwipeView *)swipeView {
    return self.bounds.size;
}

- (void)swipeViewCurrentItemIndexDidChange:(SwipeView *)swipeView {
    
    _bannerPage = (int)swipeView.currentPage;
    self.pageControl.currentPage = swipeView.currentPage;
    [self updateTitleForIndex:swipeView.currentPage];
}

- (BOOL)swipeView:(SwipeView *)swipeView shouldSelectItemAtIndex:(NSInteger)index {
    return YES;
}

- (void)swipeView:(SwipeView *)swipeView didSelectItemAtIndex:(NSInteger)index {
    if (index < self.items.count && self.cellSelectedBlock) {
        FZAdvertisementModel *item = self.items[index];
        self.cellSelectedBlock(self, item);
    }
}

@end
